package com.ossjk.oa.advisory.controller;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Channel;
import com.ossjk.oa.advisory.service.IChannelService;

/**
 * <p>
 * 校园管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/channel")
public class ChannelController extends BaseController {
	@Autowired
	private IChannelService iChannelService;

	@RequiresPermissions("00150025")
	@GetMapping("/list")
	public Object list(String name, String level, Page page) {
		Wrapper wrapper = Condition.create().ne("isdelete", 2).orderBy("slno");
		if (!CommonUtil.isBlank(name)) {
			wrapper.eq("name", name);
		}
		page = iChannelService.selectPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500250005")
	@PostMapping("/insert")
	public Object insert(Channel channel, String[] areaIds) {
		if (iChannelService.insert(channel)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500250015")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iChannelService.selectById(id));
	}

	@RequiresPermissions("001500250015")
	@PutMapping("/update")
	public Object update(Channel channel, String[] areaIds) {
		if (iChannelService.updateById(channel)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001500250010")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iChannelService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
